package club.itstudio.IContainer.controller.common;

import club.itstudio.IContainer.annotation.LoginRequired;
import club.itstudio.IContainer.annotation.RecaptchaRequired;
import club.itstudio.IContainer.entity.User;
import club.itstudio.IContainer.exception.AccountException;
import club.itstudio.IContainer.exception.BaseException;
import club.itstudio.IContainer.service.AuthService;
import club.itstudio.IContainer.service.RecaptchaService;
import club.itstudio.IContainer.service.UserService;
import club.itstudio.IContainer.util.Encrypt;
import club.itstudio.IContainer.util.Hash;
import club.itstudio.IContainer.util.Result;
import club.itstudio.IContainer.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/login")
public class LoginController {

    private UserService userService;
    private AuthService authService;
    private RecaptchaService recaptchaService;



    @GetMapping
    public void toLoginHtml(HttpServletResponse response) throws IOException {
        response.sendRedirect("/html/login.html");
    }

    @PostMapping
    @RecaptchaRequired
    public Result postLogin(HttpServletResponse response, @RequestParam String username,
                            @RequestParam String password) throws IOException {
        if (username == null || username.isEmpty()) {
            throw new AccountException();
        }
        if (password == null || password.isEmpty()) {
            throw new AccountException();
        }

        User user = userService.getByUserName(username);
        if (user == null) throw new AccountException();

        if (!user.password.equals(Hash.SHA256(password))){
            throw new AccountException();
        }

        user.lastLoginTime = System.currentTimeMillis();
        userService.updateLastLoginTime(user);

        var usernameCookie = new Cookie("username", username);
        String timestamp = String.valueOf(System.currentTimeMillis());
        var timestampCookie = new Cookie("timestamp", timestamp);
        String loginToken = Encrypt.encAES(username + timestamp, Encrypt.AESKey);
        var tokenCookie = new Cookie("token", loginToken);
        response.addCookie(usernameCookie);
        response.addCookie(timestampCookie);
        response.addCookie(tokenCookie);

        return ResultUtil.success();
    }

    @LoginRequired
    @GetMapping("/toIndex")
    public void toIndex(HttpServletResponse response) throws IOException {
        User user = authService.threadLocalUsers.get();
        switch (user.userType){
            case 0:
                response.sendRedirect("/html/admin/index.html");
                break;
            case 1:
                response.sendRedirect("/html/teacher/index.html");
                break;
            case 2:
                response.sendRedirect("/html/student/index.html");
                break;
        }
    }


    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }

    @Autowired
    public void setRecaptchaService(RecaptchaService recaptchaService) {
        this.recaptchaService = recaptchaService;
    }
}
